package com.terrier.iso
{
    import flash.geom.Point;

    public class IsoUtility
    {
        static public const THETA:Number = Math.asin(84 / 112);
        static public const SIN_THETA:Number = Math.sin(THETA);
        static public const COS_THETA_MINUS:Number = Math.cos(-THETA);
        static public const SIN_THETA_MULTIPLY_SQRT2:Number = SIN_THETA * Math.SQRT2;
        static public const COS_THETA_MINUS_MULTIPLY_SQRT2:Number = COS_THETA_MINUS * Math.SQRT2;
        static public const RECIPROCAL_SIN_THETA_HALF:Number = 0.5 / SIN_THETA;
		
		static public var SCREEN_X_OFFSET:Number;
		static public var SCREEN_Y_OFFSET:Number;
		static public var SCREEN_SCALE:Number = 1;

        /**
         * Converts a 3D point in isometric space to a 2D screen position.
         * @arg pos the 3D point.
         */
        public static function isoToScreen(pos:Vector3):Point
        {
            var screenX:Number = pos.x - pos.y;
            var screenY:Number = pos.z * COS_THETA_MINUS_MULTIPLY_SQRT2 + (pos.x + pos.y) * SIN_THETA;
            // var depth:Number = (pos.x + pos.y) * COS_THETA_MINUS - pos.z * SIN_THETA_MULTIPLY_SQRT2;
            return new Point(screenX, screenY);
        }

        /**
         * Converts a 2D screen position to a 3D point in isometric space, assuming y = 0.
         * @arg point the 2D point.
         */
        public static function screenToIso(point:Point):Vector3
        {
            var xpos:Number = point.y * RECIPROCAL_SIN_THETA_HALF + point.x * 0.5;
            var ypos:Number = point.y * RECIPROCAL_SIN_THETA_HALF - point.x * 0.5;
            var zpos:Number = 0;
            return new Vector3(xpos, ypos, zpos);
        }
    }
}
